package com.tools.cleanmaster.store;

import android.content.Context;
import android.content.SharedPreferences;

import com.tools.cleanmaster.CleanApplication;

/**
 * Created by wangguotao on 15/8/19.
 */
public class CpuCoolSharedPrefManager {
    private static final String CPU_COOL_PREFERENCE_NAME = "acc_cpu_cool_preferences";

    //上次执行降温的时间
    public static final String CPU_COOL_LAST_TIME = "_cpu_co_ls_ti";
    //上次降温之后的度数
    public static final String CPU_COOL_LAST_TEMP = "_cpu_co_ls_te";
    //上次降温的度数
    public static final String CPU_COOL_LAST_TEMP_DROP = "_cpu_co_ls_te_drop";

    private static final String JSON_KEY_TEM = "tem";

    // 默认CPU温度过高阈值
    public static final int DEFAULT_MIN_TEM = 40;

    private static SharedPreferences getSharedPreferences(Context paramContext) {
        return paramContext.getSharedPreferences(CPU_COOL_PREFERENCE_NAME,
                Context.MODE_PRIVATE);
    }

    public static long getCpuCoolLastTime() {
        SharedPreferences prefs = getSharedPreferences(CleanApplication.getInstance());
        return prefs.getLong(CPU_COOL_LAST_TIME, 0);
    }

    public static void setCpuCoolLastTime(long time) {
        Context ctx = CleanApplication.getInstance().getApplicationContext();
        SharedPreferences sharedPrefs = getSharedPreferences(ctx);
        SharedPreferences.Editor editor = sharedPrefs.edit();
        editor.putLong(CPU_COOL_LAST_TIME, time);
        editor.apply();
    }

    public static int getCpuCoolLastTemp() {
        SharedPreferences prefs = getSharedPreferences(CleanApplication.getInstance());
        return prefs.getInt(CPU_COOL_LAST_TEMP, 30);
    }

    public static void setCpuCoolLastTemp(int temp) {
        Context ctx = CleanApplication.getInstance().getApplicationContext();
        SharedPreferences sharedPrefs = getSharedPreferences(ctx);
        SharedPreferences.Editor editor = sharedPrefs.edit();
        editor.putInt(CPU_COOL_LAST_TEMP, temp);
        editor.apply();
    }

    public static int getCpuCoolLastTempDrop() {
        SharedPreferences prefs = getSharedPreferences(CleanApplication.getInstance());
        return prefs.getInt(CPU_COOL_LAST_TEMP_DROP, 5);
    }

    /**
     * 设置此次CPU降温的度数:
     * 1.正常进行降温时，设置相应的降低度数
     * 2.保护时间内，设置降低0度
     * 3.无发热应用时，设置降低0度
     *
     * @param temp CPU温度降低度数
     */
    public static void setCpuCoolLastTempDrop(int temp) {
        Context ctx = CleanApplication.getInstance().getApplicationContext();
        SharedPreferences sharedPrefs = getSharedPreferences(ctx);
        SharedPreferences.Editor editor = sharedPrefs.edit();
        editor.putInt(CPU_COOL_LAST_TEMP_DROP, temp);
        editor.apply();
    }

//    /**
//     * 获取CPU温度过高的阀值
//     */
//    public static int getTemperatureThreshold() {
//        String jsonBody = SceneSharedPrefManager.getSingleSceneConfigData(SceneType.CPU_COOLER);
//        try {
//            JSONObject jsonObject = new JSONObject(jsonBody);
//            return jsonObject.optInt(JSON_KEY_TEM, DEFAULT_MIN_TEM);
//        } catch (JSONException e) {
//            if (FeatureConfig.DEBUG_LOG) {
//                e.printStackTrace();
//            }
//        }
//        return DEFAULT_MIN_TEM;
//    }

}
